package cn.xiyue.production;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.xiyue.production.adapter.IBaseAdapter;
import cn.xiyue.production.base.BaseActivity;
import cn.xiyue.production.ui.AirActivity;
import cn.xiyue.production.ui.AutoFillActivity;
import cn.xiyue.production.ui.BuyActivity;
import cn.xiyue.production.ui.FinishedCarActivity;
import cn.xiyue.production.ui.LightActivity;
import cn.xiyue.production.ui.MarketActivity;
import cn.xiyue.production.ui.ProblemCarActivity;
import cn.xiyue.production.ui.SupplyActivity;
import cn.xiyue.production.bean.Nav;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class MainActivity extends BaseActivity {

    private GridView gv_nav;
    private List<Nav> navs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Objects.requireNonNull(getSupportActionBar()).hide();
        initView();
        initNavs();
        loadNavs();
    }

    private void initView() {
        gv_nav = (GridView) findViewById(R.id.gv_nav);
    }

    private void initNavs(){
        navs = new ArrayList<>();
        navs.add(new Nav(R.drawable.factory_lighting,"工厂灯光"));
        navs.add(new Nav(R.drawable.factory_air,"工厂空间"));
        navs.add(new Nav(R.drawable.talent_market,"人才市场"));
        navs.add(new Nav(R.drawable.auto_fill,"自动补人"));
        navs.add(new Nav(R.drawable.supply_list,"供货列表"));
        navs.add(new Nav(R.drawable.buy_yuancailiao,"购买原材料"));
        navs.add(new Nav(R.drawable.problem_car,"问题车辆仓库"));
        navs.add(new Nav(R.drawable.finished_car,"成品车辆仓库"));
    }

    private void loadNavs(){
        gv_nav.setAdapter(new IBaseAdapter<Nav>(navs) {
            @Override
            public ViewHolder<Nav> getViewHolder() {
                return new ViewHolder<Nav>() {
                    public View rootView;
                    public ImageView iv_item_img;
                    public TextView tv_item_text;
                    @Override
                    public View setData(Nav data) {
                        iv_item_img.setImageResource(data.getImage());
                        tv_item_text.setText(data.getTitle());
                        return rootView;
                    }

                    @Override
                    public View init(ViewGroup root) {
                        rootView =  LayoutInflater.from(MainActivity.this).inflate(R.layout.item_nav,root,false);
                        this.iv_item_img = (ImageView) rootView.findViewById(R.id.iv_item_img);
                        this.tv_item_text = (TextView) rootView.findViewById(R.id.tv_item_text);
                        return rootView;
                    }
                };
            }
        });
        gv_nav.setOnItemClickListener((parent, view, position, id) -> {
            Class c = null;
            switch (position){
                case 0:
                    c = LightActivity.class;
                    break;
                case 1:
                    c = AirActivity.class;
                    break;
                case 2:
                    c = MarketActivity.class;
                    break;
                case 3:
                    c = AutoFillActivity.class;
                    break;
                case 4:
                    c = SupplyActivity.class;
                    break;
                case 5:
                    c = BuyActivity.class;
                    break;
                case 6:
                    c = ProblemCarActivity.class;
                    break;
                case 7:
                    c = FinishedCarActivity.class;
                    break;
            }
            if (c!=null) startActivity(new Intent(MainActivity.this,c));
        });
    }
}